package com.amigo.online.provider.merchant.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.amigo.online.provider.merchant.mapper.GoodsImgMapper;
import com.amigo.online.provider.merchant.model.entity.GoodsImg;
import com.amigo.online.provider.merchant.service.GoodsImgService;

@Service
public class GoodsImgServiceImpl implements GoodsImgService {

    private static final Logger logger = LoggerFactory.getLogger(GoodsImgServiceImpl.class);

    @Autowired
    GoodsImgMapper goodsImgMapper;

    /**
     * 商品图片 展示图
     */
    public static final int GOODS_IMG_TYPE_SHOW = 1;

    /**
     * 商品图片 详情图
     */
    public static final int GOODS_IMG_TYPE_DETAIL = 2;


    @Override
    @Transactional()
    public void saveGoodsImgList(long goodsId, List<String> goodsImgList, List<String> goodsVideoList,
                                 List<String> goodsDetailImgList) {
        List<GoodsImg> goodsImgUrlList = new ArrayList<>();
        int i = 1;
        //图片类型，1展示图片 2详情配图
        for (String temp : goodsImgList) {
            GoodsImg goodsImg = new GoodsImg(goodsId, GOODS_IMG_TYPE_SHOW, i++, temp);
            goodsImgUrlList.add(goodsImg);
        }
        for (String temp : goodsDetailImgList) {
            GoodsImg goodsImg = new GoodsImg(goodsId, GOODS_IMG_TYPE_DETAIL, i++, temp);
            goodsImgUrlList.add(goodsImg);
        }

        int deleteResult = goodsImgMapper.deleteGoodsImgById(goodsId);
        int result = goodsImgMapper.insertList(goodsImgUrlList);
        if (result != goodsImgUrlList.size()) {
            logger.info("图片插入数量:{},实际插入数量:{}", goodsImgUrlList.size(), result);
        }
    }

}
